<template>
  <div class="aliali-announcement-card">
    <div class="card-header">
      <h3 class="card-title">
        <el-icon><Bell /></el-icon>
        公告通知
      </h3>
      <el-button type="text" size="small" @click="handleViewAll">
        更多
        <el-icon><ArrowRight /></el-icon>
      </el-button>
    </div>

    <div class="announcement-list">
      <div
          v-for="announcement in displayedAnnouncements"
          :key="announcement.id"
          class="announcement-item"
          :class="getTypeClass(announcement.type)"
          @click="handleAnnouncementClick(announcement)"
      >
        <div class="announcement-icon">
          <el-icon v-if="announcement.type === 'system'"><Setting /></el-icon>
          <el-icon v-else-if="announcement.type === 'activity'"><Promotion /></el-icon>
          <el-icon v-else><InfoFilled /></el-icon>
        </div>

        <div class="announcement-content">
          <h4 class="announcement-title">{{ announcement.title }}</h4>
          <p class="announcement-desc">{{ announcement.content }}</p>
          <div class="announcement-meta">
            <span class="announcement-time">{{ announcement.time }}</span>
          </div>
        </div>
      </div>
    </div>

    <!-- 空状态 -->
    <div v-if="!announcements.length" class="empty-announcement">
      <el-empty description="暂无公告" :image-size="60" />
    </div>
  </div>
</template>

<script setup>
import { computed } from 'vue'
import { useRouter } from 'vue-router'
import { Bell, Setting, Promotion, InfoFilled, ArrowRight } from '@element-plus/icons-vue'

const props = defineProps({
  announcements: {
    type: Array,
    default: () => []
  },
  title: {
    type: String,
    default: '公告通知'
  },
  maxDisplay: {
    type: Number,
    default: 3
  }
})

const emit = defineEmits(['announcement-click', 'view-all'])

const router = useRouter()

// 计算属性
const displayedAnnouncements = computed(() => {
  return props.announcements.slice(0, props.maxDisplay)
})

// 处理方法
const handleAnnouncementClick = (announcement) => {
  emit('announcement-click', announcement)
  if (announcement.link) {
    if (announcement.link.startsWith('http')) {
      window.open(announcement.link, '_blank')
    } else {
      router.push(announcement.link)
    }
  }
}

const handleViewAll = () => {
  emit('view-all')
  router.push('/announcement')
}

// 工具函数
const getTypeClass = (type) => {
  return `type-${type}`
}
</script>

<style scoped>
.aliali-announcement-card {
  background: white;
  border-radius: 8px;
  padding: 16px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 12px;
  padding-bottom: 8px;
  border-bottom: 1px solid #f0f0f0;
}

.card-title {
  margin: 0;
  font-size: 16px;
  font-weight: 600;
  color: #18191c;
  display: flex;
  align-items: center;
  gap: 6px;
}

.announcement-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.announcement-item {
  display: flex;
  align-items: flex-start;
  gap: 8px;
  padding: 8px;
  border-radius: 6px;
  cursor: pointer;
  transition: all 0.3s ease;
  border-left: 3px solid transparent;
}

.announcement-item:hover {
  background: #f5f7fa;
}

.announcement-item.type-system {
  border-left-color: #ff6b6b;
}

.announcement-item.type-activity {
  border-left-color: #4ecdc4;
}

.announcement-item.type-update {
  border-left-color: #45b7d1;
}

.announcement-icon {
  flex-shrink: 0;
  width: 24px;
  height: 24px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  font-size: 12px;
}

.announcement-item.type-system .announcement-icon {
  background: #ff6b6b;
}

.announcement-item.type-activity .announcement-icon {
  background: #4ecdc4;
}

.announcement-item.type-update .announcement-icon {
  background: #45b7d1;
}

.announcement-content {
  flex: 1;
  min-width: 0;
}

.announcement-title {
  margin: 0 0 2px 0;
  font-size: 12px;
  font-weight: 500;
  color: #18191c;
  line-height: 1.3;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.announcement-desc {
  margin: 0 0 4px 0;
  font-size: 11px;
  color: #666;
  line-height: 1.3;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
  height: 2.6em;
}

.announcement-meta {
  font-size: 10px;
  color: #999;
}

.empty-announcement {
  padding: 20px 0;
  text-align: center;
}
</style>